package com.ztesoft.ossup.report.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ztesoft.ossup.report.dao.ChartDataRepo;
import com.ztesoft.ossup.report.dao.ChartRepository;
import com.ztesoft.ossup.report.dao.PieChartRepo;
import com.ztesoft.ossup.report.dao.ReportColumnGroupRepo;
import com.ztesoft.ossup.report.dao.ReportColumnRepo;
import com.ztesoft.ossup.report.dao.ReportConditionEnumRepo;
import com.ztesoft.ossup.report.dao.ReportConditionRepo;
import com.ztesoft.ossup.report.dao.ReportRepository;
import com.ztesoft.ossup.report.dao.ReportSubRepo;
import com.ztesoft.ossup.report.domain.Report;

@Service("reportListService")
public class ReportListService {
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Autowired
	ReportRepository reportRepository;

	@Autowired
	ReportSubRepo reportSubRepo;
	
	@Autowired
	PieChartRepo pieChartRepo;
	@Autowired
	ChartDataRepo chartDataRepo;
	@Autowired
	ChartRepository chartRepository;
	@Autowired
	ReportColumnRepo reportColumnRepo;
	@Autowired
	ReportConditionEnumRepo reportConditionEnumRepo;
	@Autowired
	ReportConditionRepo reportConditionRepo;
	@Autowired
	ReportColumnGroupRepo reportColumnGroupRepo;
	
	@SuppressWarnings("unchecked")
	@Transactional
	public boolean deleteReport(List<Report> reportIds) throws Exception {
		boolean deleteFlag = true;
		
		for(int i = 0;i<reportIds.size();i++){
			Report report = reportIds.get(i);
			
			try {
//				//若sub表中有相关记录，删除记录
//				reportSubRepo.deleteByReportId(report.getId());
//				reportRepository.deleteById(report.getId());
				//修改状态
				reportRepository.updateReportStateById(report.getId());
			} catch (Exception e) {
				e.printStackTrace();
				deleteFlag = false;
			}
		}
		//删除report表中记录
//		reportRepository.deleteInBatch(reportIds);
		
		return deleteFlag;
	}
	
	@Transactional
	public void deleteReportCascade(List<Report> reports) throws Exception{
		
		try{
			for (Report report : reports) {//删除所有相关的表纪录
				Long reportId = report.getId();
				pieChartRepo.deletePieDataByReportId(reportId);
				chartDataRepo.deleteChartDataByReportId(reportId);
				chartRepository.deleteByReportId(reportId);
				reportConditionEnumRepo.deleteConditionEnumByReportId(reportId);
				reportConditionRepo.deleteByReportId(reportId);
				reportColumnGroupRepo.deleteByReportId(reportId);
				reportColumnRepo.deleteByReportId(reportId);
				reportSubRepo.deleteByReportId(reportId);
				reportRepository.delete(reportId);
			}
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		
	}
	
}
